package com.samsungmobile.common.util;

import java.util.StringTokenizer;
import java.util.Vector;

public class EasyString
{

    private EasyString()
    {
    }

    public static String substring(String s, int i, int j)
    {
        try
        {
            if(j > s.length())
                j = s.length();
            return s.substring(i, j);
        }
        catch(IndexOutOfBoundsException indexoutofboundsexception)
        {
            return "";
        }
    }

    public static String left(String s, int i)
    {
        try
        {
            if(i > s.length())
                i = s.length();
            return s.substring(0, i);
        }
        catch(IndexOutOfBoundsException indexoutofboundsexception)
        {
            return "";
        }
    }

    public static String right(String s, int i)
    {
        try
        {
            int j = s.length() - i;
            if(j < 0)
                j = 0;
            return s.substring(j);
        }
        catch(IndexOutOfBoundsException indexoutofboundsexception)
        {
            return "";
        }
    }

    public static String replace(String s, String s1, String s2)
    {
        if(s == null)
            return null;
        StringBuffer stringbuffer = new StringBuffer("");
        int i = s1.length();
        int j = s.length();
        boolean flag = false;
        int k;
        int l;
        for(l = 0; (k = s.indexOf(s1, l)) >= 0; l = k + i)
        {
            stringbuffer.append(s.substring(l, k));
            stringbuffer.append(s2);
        }

        if(l < j)
            stringbuffer.append(s.substring(l, j));
        return stringbuffer.toString();
    }

    public static String fillString(String s, int i)
    {
        StringBuffer stringbuffer = new StringBuffer();
        for(int j = 0; j < i; j++)
            stringbuffer.append(s);

        return stringbuffer.toString();
    }

    public static int fillInteger(int i, int j)
    {
        String s = String.valueOf(i);
        s = fillString(s, j);
        return Integer.parseInt(s);
    }

    public static String fillSpace(String s, int i)
    {
        return fillRight(s, i, ' ');
    }

    public static String fillZero(int i, int j)
    {
        return fillLeft(String.valueOf(i), j, '0');
    }

    public static String fillRight(String s, int i, char c)
    {
        if(s == null)
        {
            return "";
        } else
        {
            StringBuffer stringbuffer = new StringBuffer();
            stringbuffer.append(s);
            stringbuffer.append(fillString(String.valueOf(c), i - s.length()));
            return stringbuffer.toString();
        }
    }

    public static String fillLeft(String s, int i, char c)
    {
        String s1 = s;
        int j = s.length();
        s1 = fillString(String.valueOf(c), i - j) + s;
        return s1;
    }

    public static int parseInt(String s, int i)
    {
        try
        {
            return Integer.parseInt(s);
        }
        catch(NumberFormatException numberformatexception)
        {
            return i;
        }
    }

    public static int parseInt(String s)
    {
        return parseInt(s, -1);
    }

    public static boolean parseBoolean(String s)
    {
        boolean flag = true;
        if(s == null)
            flag = false;
        return flag;
    }

    public static String toString(String s)
    {
        if(s == null)
            return "";
        else
            return s;
    }

    public static String toString(String s, String s1)
    {
        if(s == null || s.equals(""))
            return s1;
        else
            return s;
    }

    public static Vector stringTokenizer(String s, String s1)
    {
        return StringTokenizer(s, s1, false);
    }

    public static Vector split(String s, String s1)
    {
        return StringTokenizer(s, s1, false);
    }

    public static Vector StringTokenizer(String s, String s1, boolean flag)
    {
        if(s == null || s1 == null)
            return null;
        Vector vector = new Vector(5, 5);
        String s2;
        for(StringTokenizer stringtokenizer = new StringTokenizer(s, s1, flag); stringtokenizer.hasMoreElements(); vector.addElement(s2))
            s2 = stringtokenizer.nextToken();

        return vector;
    }

    public static String[] StringTokenizer(String s, String s1)
    {
        int i = 0;
        int j = 0;
        int k = s.length();
        StringArray stringarray = new StringArray();
        do
        {
            int l = s.indexOf(s1, i);
            if(l == -1)
            {
                if(i < k)
                {
                    stringarray.addElement(s.substring(i).trim());
                    j++;
                }
                break;
            }
            if(i < k)
            {
                stringarray.addElement(s.substring(i, l).trim());
                j++;
            }
            i = l + s1.length();
        } while(true);
        return j != 0 ? stringarray.toArray() : null;
    }

    public static String[] splitToStringArray(String s, String s1)
    {
        return StringTokenizer(s, s1);
    }

    public static String addComma(int i)
    {
        String s = "";
        String s1 = "";
        String s2 = String.valueOf(i);
        int j = s2.length();
        int k = 0;
        for(int l = j - 1; l >= 0; l--)
        {
            s = s + s2.substring(l, l + 1);
            if(k % 3 == 2 && l > 0)
                s = s + ",";
            k++;
        }

        j = s.length();
        for(int i1 = j - 1; i1 >= 0; i1--)
            s1 = s1 + s.substring(i1, i1 + 1);

        return s1;
    }

    public static int getByteLength(String s)
    {
        byte abyte0[] = s.getBytes();
        return abyte0.length;
    }

    public static int getByteLength(char c)
    {
        String s = new String(c + "");
        byte abyte0[] = s.getBytes();
        return abyte0.length;
    }

    public static boolean isHanCode(char c)
    {
        if(c >= 0 && c <= '\377')
            return false;
        return c >= '\uAC00' && c <= '\uD7A3';
    }

    public static String cutByteString(String s, int i)
    {
        return cutByteString(s, i, null);
    }

    public static String[] cutByteStringToArray(String s, int i)
    {
        int j = getByteLength(s);
        String as[] = new String[(j - 1) / i + 1];
        if(s == null)
            return as;
        char ac[] = s.toCharArray();
        StringBuffer stringbuffer = new StringBuffer();
        int k = i;
        int l = 0;
        boolean flag = false;
        int j1 = 0;
        for(int k1 = 0; k1 < ac.length; k1++)
        {
            int i1 = getByteLength(ac[k1]);
            l += i1;
            if(k >= l)
            {
                stringbuffer.append(ac[k1]);
            } else
            {
                as[j1] = stringbuffer.toString();
                stringbuffer.delete(0, stringbuffer.length());
                j1++;
            }
        }

        return as;
    }

    public static Vector cutByteStringToVector(String s, int i)
    {
        int j = getByteLength(s);
        System.out.println((j - 1) / i + 1);
        Vector vector = new Vector();
        if(s == null)
            return vector;
        char ac[] = s.toCharArray();
        StringBuffer stringbuffer = new StringBuffer();
        int k = i;
        int l = 0;
        boolean flag = false;
        int j1 = 0;
        for(int k1 = 0; k1 < ac.length; k1++)
        {
            int i1 = getByteLength(ac[k1]);
            l += i1;
            if(k >= l)
            {
                stringbuffer.append(ac[k1]);
            } else
            {
                vector.addElement(stringbuffer.toString());
                stringbuffer.delete(0, stringbuffer.length());
                j1++;
                k1--;
                l = 0;
            }
        }

        if(stringbuffer.length() > 0)
            vector.addElement(stringbuffer.toString());
        return vector;
    }

    public static String cutByteString(String s, int i, String s1)
    {
        if(s == null)
            return "";
        char ac[] = s.toCharArray();
        StringBuffer stringbuffer = new StringBuffer();
        int j = getByteLength(s);
        int k;
        if(s1 == null)
            k = 0;
        else
            k = getByteLength(s1);
        if(j <= i)
            return s;
        int l = i - k;
        int i1 = 0;
        boolean flag = false;
        for(int k1 = 0; k1 < ac.length; k1++)
        {
            int j1 = getByteLength(ac[k1]);
            i1 += j1;
            if(l >= i1)
                stringbuffer.append(ac[k1]);
        }

        if(s1 != null)
            stringbuffer.append(s1);
        return stringbuffer.toString();
    }

    public static String cutQuote(String s)
    {
        if(s == null || s.length() < 1)
            return s;
        s = s.trim();
        int i = s.length() - 1;
        if(i < 2)
            return s;
        if(s.charAt(0) == '"' && s.charAt(i) == '"' || s.charAt(0) == '\'' && s.charAt(i) == '\'')
            s = s.substring(1, i);
        return s;
    }

    public static String changeFirstChartoUpperCase(String s)
    {
        String s1 = s.substring(0, 1).toUpperCase();
        String s2 = s1.concat(s.substring(1));
        return s2;
    }

    public static boolean isSet(String s)
    {
        return s != null && s.length() > 0;
    }

    public static String makeRandomString()
    {
        return makeRandomString(8);
    }

    public static String makeRandomString(int i)
    {
        byte abyte0[] = (new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")).getBytes();
        return makeRandomString(i, abyte0);
    }

    public static String makeRandomString(int i, String s)
    {
        return makeRandomString(i, s.getBytes());
    }

    public static String makeRandomString(int i, byte abyte0[])
    {
        byte abyte1[] = new byte[i];
        for(int j = 0; j < i; j++)
            abyte1[j] = abyte0[(int)(Math.random() * (double)abyte0.length)];

        return new String(abyte1);
    }

    public int getWordCounts(String s, String s1)
    {
        int i = 0;
        int j = 0;
        int k = s1.length();
        do
        {
            try
            {
                i = s.indexOf(s1, i);
                if(i == -1)
                    break;
                i = k + i;
                j++;
                continue;
            }
            catch(Exception exception) { }
            break;
        } while(true);
        return j;
    }

    public static String insert(String s, int i, String s1)
    {
        StringBuffer stringbuffer = new StringBuffer();
        int j = s.length();
        if(i >= 0)
        {
            if(j < i)
                i = j;
            stringbuffer.append(s.substring(0, i));
            stringbuffer.append(s1);
            stringbuffer.append(s.substring(i + s1.length()));
        } else
        {
            if(j < Math.abs(i))
                i = j * -1;
            stringbuffer.append(s.substring(0, (j - 1) + i));
            stringbuffer.append(s1);
            stringbuffer.append(s.substring((j - 1) + i + s1.length()));
        }
        return new String(stringbuffer);
    }

    public static boolean isFullWord(String s)
    {
        byte abyte0[] = null;
        abyte0 = s.getBytes();
        for(int i = 0; i < abyte0.length; i++)
            if(abyte0[i] >= -127 && abyte0[i] <= -97 || abyte0[i] >= -32 && abyte0[i] <= -17)
            {
                if(abyte0[i + 1] >= 64 && abyte0[i + 1] <= 126 || abyte0[i + 1] >= -128 && abyte0[i + 1] <= -4)
                    i++;
                else
                    return false;
            } else
            {
                return false;
            }

        return true;
    }

    public static boolean isHalfWord(String s)
    {
        byte abyte0[] = null;
        abyte0 = s.getBytes();
        for(int i = 0; i < abyte0.length; i++)
            if((abyte0[i] >= -127 && abyte0[i] <= -97 || abyte0[i] >= -32 && abyte0[i] <= -17) && (abyte0[i + 1] >= 64 && abyte0[i + 1] <= 126 || abyte0[i + 1] >= -128 && abyte0[i + 1] <= -4))
                return false;

        return true;
    }
    
    public static String paramToString(String strParam)
	{
	    if (strParam==null || strParam.indexOf("<") > -1 || strParam.indexOf(">") > -1 || strParam.indexOf("\"") > -1)
	    {
	        strParam = "";
	    }	    
	    return strParam;
	}
}
